﻿// JavaScript source code
"use strict";
//定义圆角矩形方块的原型
CanvasRenderingContext2D.prototype.cirRct = function (x, y, w, h, r, color) {
    this.beginPath();
    this.moveTo(x + r, y);
    this.lineTo(x + w - r, y);
    this.arcTo(x + w, y, x + w, y + r, r);
    this.lineTo(x + w, y + h - r);
    this.arcTo(x + w, y + h, x + w - r, y + h, r);
    this.lineTo(x + r, y + h);
    this.arcTo(x, y + h, x, y + h - r, r);
    this.lineTo(x, y + r);
    this.arcTo(x, y, x + r, y, r);
    this.fillStyle = color;
    this.closePath();
    this.fill();
    this.stroke();
}
//定义画出坦克的原型方法
CanvasRenderingContext2D.prototype.drawTank = function (x, y, l, d, color) {
    var l1 = 0.5 * l, w1 = 0.2 * l, l2 = 0.4 * l, w2 = 0.3 * l, l3 = 0.2 * l, w3 = 0.2 * l, l4 = 0.6 * l, w4 = 0.3 * l;
    var spaceW = l - w2 - 2 * w4;
    if (d == 1) {//上
        //画出两个车轮
        this.cirRct(x + spaceW / 2, y + l - l4, w4, l4, 0, color);
        this.cirRct(x + (l + w2) / 2, y + l - l4, w4, l4, 0, color);
        //画出炮口
        this.cirRct(x + spaceW / 2 + w4 + (w2 - w3) / 2, y, w1, l1, 0, color);
        //画出炮身
        this.cirRct(x + spaceW / 2 + w4, y + l1, w2, l2, 0, color);
        //画出炮舱
        this.cirRct(x + spaceW / 2 + w4 + (w2 - w3) / 2, y + l1 + (l2 - l3) / 2, w3, l3, 0, color);
    }
    else if (d == 2) {//右
        this.cirRct(x, y + spaceW / 2, l4, w4, 0, color);
        this.cirRct(x, y + spaceW / 2 + w2 + w4, l4, w4, 0, color);
        this.cirRct(x + l - l1, y + spaceW / 2 + w4, l1, w1, 0, color);
        this.cirRct(x + l - l1 - l2, y + spaceW / 2 + w4, l2, w2, 0, color);
        this.cirRct(x + l - l1 - (l2 + l3) / 2, y + spaceW / 2 + w4 + (w2 - w3) / 2, l3, w3, 0, color);
    }
    else if (d == 3) {//下
        this.cirRct(x + spaceW / 2, y, w4, l4, 0, color);
        this.cirRct(x + (l + w2) / 2, y, w4, l4, 0, color);
        this.cirRct(x + spaceW / 2 + w4 + (w2 - w3) / 2, y + l - l1, w1, l1, 0, color);
        this.cirRct(x + spaceW / 2 + w4, y + l - l1 - l2, w2, l2, 0, color);
        this.cirRct(x + spaceW / 2 + w4 + (w2 - w3) / 2, y + l - l1 - (l2 + l3) / 2, w3, l3, 0, color);
    }
    else if (d == 4) {//左
        this.cirRct(x + l - l4, y + spaceW / 2, l4, w4, 0, color);
        this.cirRct(x + l - l4, y + (l + w2) / 2, l4, w4, 0, color);
        this.cirRct(x, y + spaceW / 2 + w4 + (w2 - w3) / 2, l1, w1, 0, color);
        this.cirRct(x + l1, y + spaceW / 2 + w4, l2, w2, 0, color);
        this.cirRct(x + l1 + (l2 - l3) / 2, y + spaceW / 2 + w4 + (w2 - w3) / 2, l3, w3, 0, color);
    }
}
//定义画出旗的方法
CanvasRenderingContext2D.prototype.drawFlag = function (x, y, l) {
    this.cirRct(x + l * 0.1, y, l * 0.75, l / 2, 0, 'rgb(255,0,0)');
    this.beginPath();
    this.moveTo(x + l * 0.1 + 1, y + l / 2);
    this.lineTo(x + l * 0.1 + 1, y + l);
    this.strokeStyle = 'rgb(255,255,255)';
    this.stroke();
    this.closePath();
    this.strokeStyle = 'rgb(0,0,0)';
}
//画出炮弹
CanvasRenderingContext2D.prototype.drawBullets = function (x, y, l, color) {
    this.cirRct(x, y, l, l, 0, color);
}
//画出道具
CanvasRenderingContext2D.prototype.drawProp = function (x, y, l, index, randomProp) {
    this.cirRct(x, y, l, l, 1, randomProp[index].c);
    this.fillStyle = 'rgb(0,0,0)';//设置字体颜色
    this.textAlign = 'center';
    this.textBaseline = 'middle';
    this.font = '8px Adobe Ming Std';
    this.fillText(randomProp[index].t, x + l / 2, y + l / 2);
}

function tankGame() {
    this.blocksize = 20;//格子大小
    //定义炮弹数组
    this.bullets = [];
    //定义坦克初始位置
    this.tanksPosition = [
        { x: 220, y: canvas.height - 40 },
        { x: 340, y: canvas.height - 40 }
    ]
    //定义道具标签
    this.randomProp = [{ t: 'T+', c: 'rgb(102,204,204)' }, { t: 'T-', c: 'rgb(0,204,51)' }, { t: 'H+', c: 'rgb(204,102,0)' }, { t: 'V+', c: 'rgb(204,0,204)' }, { t: 'S+', c: 'rgb(102,102,102)' }, { t: 'S-', c: 'rgb(204,153,204)' }];
    //定义道具数组
    this.props = [];
    //定义道具长度
    this.propLength = 15;
    //定义坦克数组
    //x,y:坦克坐标;v:坦克移动的速度,l:坦克的边长,h:坦克的生命值,d:坦克的方向（1：上，2：右，3：下，4：左）,i:坦克是否可以发射炮弹（0：是，1：否）,up,right,down,left,shot:坦克各方向按键对应键码及按键状态,t:炮弹每t毫秒移动一次,inter:坦克发射炮弹的时间间隔,color:坦克颜色
    this.tanks = [
        { x: 220, y: canvas.height - 40, v: 5, l: 40, h: 3, d: 1, i: 0, up: [38, false], right: [39, false], down: [40, false], left: [37, false], shot: [32, false], t: 6, inter: 500, color: 'rgb(0,204,204)' },
        { x: 340, y: canvas.height - 40, v: 5, l: 40, h: 3, d: 1, i: 0, up: [87, false], right: [68, false], down: [83, false], left: [65, false], shot: [192, false], t: 6, inter: 500, color: 'rgb(0,255,0)' }
    ];
    //添加AI坦克
    this.tankAiModel = [
        { v: 3, h: 2, l: 25, inter: 2000, t: 10, color: 'rgb(255,0,0)' },//炮弹t毫秒移动一次
        { v: 3, h: 0, l: 15, inter: 2000, t: 4, color: 'rgb(204,204,204)' },
        { v: 3, h: 0, l: 20, inter: 2000, t: 8, color: 'rgb(204,204,204)' }
    ];
    //初始化坦克同一时间出现数量
    this.tankDisplay = 5;
    //储存坦克AI的数组
    this.tanksAi = [];
    //初始化摧毁数
    this.kills = 0;
    this.kill = [0, 0]
    //初始化胜利所需摧毁的坦克数
    this.wins = 15;
    //宣布游戏结束
    this.gameover = false;
    //更新数据
    this.updateData = function (tanks, bullets, tanksAi) {
        this.tanks = tanks;
        this.bullets = bullets;
        this.tanksAi = tanksAi;
    }
    //更新分数
    this.updateMark = function () {
        var objTanks = this.tanks;
        for (var i = 0; i < objTanks.length; i++) {
            $("#healthy" + i + "_con").html(objTanks[i].h);
            $("#speed" + i + "_con").html(objTanks[i].v);
            $("#positionX" + i + "_con").html(objTanks[i].x);
            $("#positionY" + i + "_con").html(objTanks[i].y);
            $("#direction" + i + "_con").html(objTanks[i].d);
            $("#isShoot" + i + "_con").html(objTanks[i].i);
            $("#bulletSpeed" + i + "_con").html(objTanks[i].t);
            $("#bulletDistance" + i + "_con").html(objTanks[i].inter);
        }
    }
    //判断两物体是否碰撞
    this.judgeCrash = function (x1, y1, l1, x2, y2, l2) {//两个物体的左上角坐标和边长
        if (l1 > l2) {
            if ((x2 >= x1) && (x2 <= x1 + l1) && (y2 >= y1) && (y2 <= y1 + l1))
                return true;
            else if ((x2 + l2 >= x1) && (x2 + l2 <= x1 + l1) && (y2 >= y1) && (y2 <= y1 + l1))
                return true;
            else if ((x2 >= x1) && (x2 <= x1 + l1) && (y2 + l2 >= y1) && (y2 + l2 <= y1 + l1))
                return true;
            else if ((x2 + l2 >= x1) && (x2 + l2 <= x1 + l1) && (y2 + l2 >= y1) && (y2 + l2 <= y1 + l1))
                return true;
        }
        else {
            if ((x1 >= x2) && (x1 <= x2 + l2) && (y1 >= y2) && (y1 <= y2 + l2))
                return true;
            else if ((x1 + l1 >= x2) && (x1 + l1 <= x2 + l2) && (y1 >= y2) && (y1 <= y2 + l2))
                return true;
            else if ((x1 >= x2) && (x1 <= x2 + l2) && (y1 + l1 >= y2) && (y1 + l1 <= y2 + l2))
                return true;
            else if ((x1 + l1 >= x2) && (x1 + l1 <= x2 + l2) && (y1 + l1 >= y2) && (y1 + l1 <= y2 + l2))
                return true;
        }
        return false;
    }
    //射击间隔
    this.shotInterval = function (i, tanks) {//坦克的索引，对应的坦克数组
        setTimeout(function () {
            if (tanks[i])
                tanks[i].i--;
        }, tanks[i].inter);
    }
    //判断随机数出现是否重叠
    this.judgeRepeat = function (x, r) {//坦克的x坐标和边长
        var tanksAi = this.tanksAi;
        var tankAiModel = this.tankAiModel;
        for (var i = 0, len = tanksAi.length; i < len; i++) {
            if (this.judgeCrash(x, 0, tankAiModel[r].l, tanksAi[i].x, tanksAi[i].y, tanksAi[i].l))
                return true;//重叠
        }
        return false;
    }
    //判断是否超出容器
    this.outOfcContainer = function (x, y, l, vx, vy, width, height) { //物体的x,y坐标，边长，速度，容器宽高
        if ((x + vx < 0) || (x + l + vx > width))
            return true;
        else if ((y + vy < 0) || (y + l + vy > height))
            return true;
        else
            return false;
    }
    //计算剩余存活的坦克数量
    this.AIalive = function () {
        var a = 0;
        var tanksAi = this.tanksAi;
        for (var i = 0, len = tanksAi.length; i < len; i++) {
            if (tanksAi[i].h >= 0)
                a++;
        }
        return a;
    }
}

tankGame.prototype = new base();
//实例化对象
var tankgame = new tankGame();

//给己方坦克x y位置赋值 
function Cell(x, y) {
    //tanksPosition
    this.x = x;
    this.y = y;
}

//动态页面处理
tankGame.prototype.dynamicPage = function () {
    //自动生成option
    for (var i = 0; i < 10; i++) {
        $("<option></option>").attr("value", i + 1).html(i + 1).appendTo($("#propsFreq_con"));
    }
    //默认选择的option
    $("#propsFreq_con").val(4);
}

//初始化
tankGame.prototype.init = function () {
}

//绘制画面
tankGame.prototype.draw = function (context) {
    var objTanks = this.tanks;
    var tanksAi = this.tanksAi;
    var objBullets = this.bullets;
    var props = this.props;
    this.woodContrust();
    this.FeContrust();
    this.protectContrust();
    this.flagContrust();
    //画出坦克
    for (var i = 0, len = objTanks.length; i < len; i++) {
        context.drawTank(objTanks[i].x, objTanks[i].y, 40, objTanks[i].d, objTanks[i].color);
    }
    //画出道具
    if (props.length > 0) {
        context.drawProp(props[0].x, props[0].y, this.propLength, props[0].t, this.randomProp);
    }
    //画出AI坦克
    for (var k = 0, alen = tanksAi.length; k < alen; k++) {
        context.drawTank(tanksAi[k].x, tanksAi[k].y, tanksAi[k].l, tanksAi[k].d, tanksAi[k].color);
        this.AIshot(k);
    }
    //context.drawTank(objTanks[0].x, objTanks[0].y, 40, objTanks[0].d, objTanks[0].color);
    //画出炮弹
    for (var j = 0, blen = objBullets.length; j < blen; j++) {
        context.drawBullets(objBullets[j].x, objBullets[j].y, objBullets[j].l, objBullets[j].color);
    }
    if (tanksAi.length > 0) this.AImove();
    if (this.AIalive() < this.tankDisplay) this.tankAiProduce();
    //ToDo: 画出坦克之家
    //drawHome();
}
//移动机制
tankGame.prototype.ikeydown = function (keyCode) {
    var objTanks = this.tanks;
    var objBullets = this.bullets;
    var blocksize = this.blocksize;
    var props = this.props;
    switch (keyCode) {
        //玩家一
        //上 W
        case 87:
            if (objTanks[0].y - blocksize < 0) break;
            objTanks[0].y = objTanks[0].y - blocksize;
            objTanks[0].d = 1;
            break;
        //下 S
        case 83:
            if (objTanks[0].y + blocksize + objTanks[0].l > this.canvas.height) break;
            objTanks[0].y = objTanks[0].y + blocksize;
            objTanks[0].d = 3;
            break;
        //左 A
        case 65:
            if (objTanks[0].x - blocksize < 0) break;
            objTanks[0].x = objTanks[0].x - blocksize;
            objTanks[0].d = 4;
            break;
        //右 D
        case 68:
            if (objTanks[0].x + blocksize + objTanks[0].l > this.canvas.width) break;
            objTanks[0].x = objTanks[0].x + blocksize;
            objTanks[0].d = 2;
            break;
        //空格射击
        case 32:
            if (objTanks[0].i == 0) {
                objTanks[0].i = 1;
                this.shotInterval(0, objTanks);
                objBullets[objBullets.length] = { x: objTanks[0].x + objTanks[0].l * 3 / 8, y: objTanks[0].y + objTanks[0].l * 3 / 8, l: objTanks[0].l / 4, t: objTanks[0].t, color: 'rgb(255,0,0)', belong: 1 };
                //x,y:炮弹的坐标，l:炮弹的边长，t:炮弹每隔t毫秒移动一次，color:炮弹的颜色，炮弹所属坦克
                if (objTanks[0].d == 1) {
                    objBullets[objBullets.length - 1].y = objTanks[0].y - objTanks[0].l / 4;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 0, -1, objBullets.length - 1);
                }
                else if (objTanks[0].d == 2) {
                    objBullets[objBullets.length - 1].x = objTanks[0].x + objTanks[0].l;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 1, 0, objBullets.length - 1);
                }
                else if (objTanks[0].d == 3) {
                    objBullets[objBullets.length - 1].y = objTanks[0].y + objTanks[0].l;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 0, 1, objBullets.length - 1);
                }
                else if (objTanks[0].d == 4) {
                    objBullets[objBullets.length - 1].x = objTanks[0].x - objTanks[0].l / 4;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], -1, 0, objBullets.length - 1);
                }
            }
            break;
        //玩家二
        //上
        case 38:
            if (objTanks[1].y - blocksize < 0) break;
            objTanks[1].y = objTanks[1].y - blocksize;
            objTanks[1].d = 1;
            break;
        //右
        case 39:
            if (objTanks[1].x + blocksize + objTanks[1].l > this.canvas.width) break;
            objTanks[1].x = objTanks[1].x + blocksize;
            objTanks[1].d = 2;
            break;
        //下
        case 40:
            if (objTanks[1].y + blocksize + objTanks[1].l > this.canvas.height) break;
            objTanks[1].y = objTanks[1].y + blocksize;
            objTanks[1].d = 3;
            break;
        //左
        case 37:
            if (objTanks[1].x - blocksize < 0) break;
            objTanks[1].x = objTanks[1].x - blocksize;
            objTanks[1].d = 4;
            break;
        //数字0 射击
        case 96:
            if (objTanks[1].i == 0) {
                objTanks[1][1].i = 1;
                this.shotInterval(1, objTanks[1]);
                objBullets[objBullets.length] = { x: objTanks[1].x + objTanks[1].l * 3 / 8, y: objTanks[1].y + objTanks[1].l * 3 / 8, l: objTanks[1].l / 4, t: objTanks[1].t, color: 'rgb(255,0,0)', belong: 2 };
                //x,y:炮弹的坐标，l:炮弹的边长，t:炮弹每隔t毫秒移动一次，color:炮弹的颜色，炮弹所属坦克
                if (objTanks[1].d == 1) {
                    objBullets[objBullets.length - 1].y = objTanks[1].y - objTanks[1].l / 4;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 0, -1, objBullets.length - 1);
                }
                else if (objTanks[1].d == 2) {
                    objBullets[objBullets.length - 1].x = objTanks[1].x + objTanks[1].l;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 1, 0, objBullets.length - 1);
                }
                else if (objTanks[1].d == 3) {
                    objBullets[objBullets.length - 1].y = objTanks[1].y + objTanks[1].l;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], 0, 1, objBullets.length - 1);
                }
                else if (objTanks[1].d == 4) {
                    objBullets[objBullets.length - 1].x = objTanks[1].x - objTanks[1].l / 4;
                    this.bulletsMove(0, objBullets[objBullets.length - 1], -1, 0, objBullets.length - 1);
                }
            }
            break;
    }
    //碰撞道具后的处理， 还有没解决的， 还剩下玩家二的处理
    if (props.length > 0) {
        if (this.judgeCrash(objTanks[0].x, objTanks[0].y, objTanks[0].l, props[0].x, props[0].y, this.propLength)) {
            this.propsApply(0);
            props.length = 0;
        }
    }
    this.draw(this.context);
}

//坦克的移动
//坦克索引，坦克在x,y正方向上的移动，（0:不移动,1:向正方向移动,-1:向反方向移动)，坦克类型
tankGame.prototype.tanksMove = function (i, dx, dy, type) {
    var tanksAi = this.tanksAi;
    var tanks = this.tanks;
    var woods = this.woods;
    var woodLength = this.woodLength;
    var protects = this.protects;
    var Fes = this.Fes;
    var pic = this.pic;
    var Felength = this.Felength;
    if (type == 'AI') {
        tanksAi[i].x += tanksAi[i].v * dx;
        tanksAi[i].y += tanksAi[i].v * dy;
        for (var j = 0, len = tanksAi.length; j < len; j++) {
            if ((i != j) && (this.judgeCrash(tanksAi[i].x, tanksAi[i].y, tanksAi[i].l, tanksAi[j].x, tanksAi[j].y, tanksAi[j].l))) {
                tanksAi[i].x -= tanksAi[i].v * dx;
                tanksAi[i].y -= tanksAi[i].v * dy;
                break;
            }
        }
        for (var w = 0, wlen = woods[pic].length; w < wlen; w++) {
            if (this.judgeCrash(tanksAi[i].x, tanksAi[i].y, tanksAi[i].l, woods[pic][w].x * woodLength, woods[pic][w].y * woodLength, woodLength)) {
                tanksAi[i].x -= tanksAi[i].v * dx;
                tanksAi[i].y -= tanksAi[i].v * dy;
                break;
            }
        }
        for (var f = 0, flen = Fes[pic].length; f < flen; f++) {
            if (this.judgeCrash(tanksAi[i].x, tanksAi[i].y, tanksAi[i].l, Fes[pic][f].x * Felength, Fes[pic][f].y * Felength, Felength)) {
                tanksAi[i].x -= tanksAi[i].v * dx;
                tanksAi[i].y -= tanksAi[i].v * dy;
                break;
            }
        }
        for (var p = 0, plen = tanks.length; p < plen; p++) {
            if (this.judgeCrash(tanksAi[i].x, tanksAi[i].y, tanksAi[i].l, tanks[p].x, tanks[p].y, tanks[p].l)) {
                tanksAi[i].x -= tanksAi[i].v * dx;
                tanksAi[i].y -= tanksAi[i].v * dy;
                break;
            }
        }
        for (var pr = 0, prlen = protects.length; pr < prlen; pr++) {
            if (this.judgeCrash(tanksAi[i].x, tanksAi[i].y, tanksAi[i].l, protects[pr].x * woodLength, protects[pr].y * woodLength, woodLength)) {
                tanksAi[i].x -= tanksAi[i].v * dx;
                tanksAi[i].y -= tanksAi[i].v * dy;
                break;
            }
        }
        if ((tanksAi[i].x < 0) || (tanksAi[i].x + tanksAi[i].l > this.canvas.width) || (tanksAi[i].y < 0) || (tanksAi[i].y + tanksAi[i].l > this.canvas.height)) {
            tanksAi[i].x -= tanksAi[i].v * dx;
            tanksAi[i].y -= tanksAi[i].v * dy;
        }
    }
    else {
        tanks[i].x += (tanks[i].v * dx);
        tanks[i].y += (tanks[i].v * dy);
        for (var j = 0, len = tanksAi.length; j < len; j++) {
            if (judgeCrash(tanks[i].x, tanks[i].y, tanks[i].l, tanksAi[j].x, tanksAi[j].y, tanksAi[j].l)) {
                tanks[i].x -= (tanks[i].v * dx);
                tanks[i].y -= (tanks[i].v * dy);
                break;
            }
        }
        for (var w = 0, wlen = woods[pic].length; w < wlen; w++) {
            if (judgeCrash(tanks[i].x, tanks[i].y, tanks[i].l, woods[pic][w].x * woodLength, woods[pic][w].y * woodLength, woodLength)) {
                tanks[i].x -= (tanks[i].v * dx);
                tanks[i].y -= (tanks[i].v * dy);
                break;
            }
        }
        for (var f = 0, flen = Fes[pic].length; f < flen; f++) {
            if (judgeCrash(tanks[i].x, tanks[i].y, tanks[i].l, Fes[pic][f].x * Felength, Fes[pic][f].y * Felength, Felength)) {
                tanks[i].x -= (tanks[i].v * dx);
                tanks[i].y -= (tanks[i].v * dy);
                break;
            }
        }
        if (props.length > 0) {
            if (judgeCrash(tanks[i].x, tanks[i].y, tanks[i].l, props[0].x, props[0].y, propLength)) {
                propsApply(i);
                props.length = 0;
            }
        }
        for (var pr = 0, prlen = protects.length; pr < prlen; pr++) {
            if (judgeCrash(tanks[i].x, tanks[i].y, tanks[i].l, protects[pr].x * woodLength, protects[pr].y * woodLength, woodLength)) {
                tanks[i].x -= tanks[i].v * dx;
                tanks[i].y -= tanks[i].v * dy;
                break;
            }
        }
        if ((tanks[i].x < 0) || (tanks[i].x + tanks[i].l > canvas.width) || (tanks[i].y < 0) || (tanks[i].y + tanks[i].l > canvas.height)) {
            tanks[i].x -= tanks[i].v * dx;
            tanks[i].y -= tanks[i].v * dy;
        }
    }
}

//AI坦克的移动
tankGame.prototype.AImove = function () {
    var Rd = Math.floor(Math.random() * 4) + 1;
    var tanksAiX, tanksAiY;
    var tanksAi = this.tanksAi;
    for (var i = 0, len = tanksAi.length; i < len; i++) {
        if (tanksAi[i].x < this.canvas.width) {
            tanksAiX = tanksAi[i].x;
            tanksAiY = tanksAi[i].y;
            if (tanksAi[i].d == 1) {
                this.tanksMove(i, 0, -1, 'AI');
                if ((tanksAiX == tanksAi[i].x) && (tanksAiY == tanksAi[i].y))
                    tanksAi[i].d = Rd;
            }
            else if (tanksAi[i].d == 2) {
                this.tanksMove(i, 1, 0, 'AI');
                if ((tanksAiX == tanksAi[i].x) && (tanksAiY == tanksAi[i].y))
                    tanksAi[i].d = Rd;
            }
            else if (tanksAi[i].d == 3) {
                this.tanksMove(i, 0, 1, 'AI');
                if ((tanksAiX == tanksAi[i].x) && (tanksAiY == tanksAi[i].y))
                    tanksAi[i].d = Rd;
            }
            else if (tanksAi[i].d == 4) {
                this.tanksMove(i, -1, 0, 'AI');
                if ((tanksAiX == tanksAi[i].x) && (tanksAiY == tanksAi[i].y))
                    tanksAi[i].d = Rd;
            }
        }
    }
}

//生成AI坦克
tankGame.prototype.tankAiProduce = function () {
    var tanksAi = this.tanksAi;
    var tankAiModel = this.tankAiModel;
    var Rform = Math.floor(Math.random() * tankAiModel.length),
        Rd = Math.floor(Math.random() * 4) + 1,
        Rx = Math.floor(Math.random() * canvas.width);
    if (Rx > canvas.width - tankAiModel[Rform].l)
        Rx = canvas.width - tankAiModel[Rform].l;
    while (this.judgeRepeat(Rx, Rform)) {
        Rx = (Math.random() * canvas.width);
        if (Rx > canvas.width - tankAiModel[Rform].l)
            Rx = canvas.width - tankAiModel[Rform].l;
    }
    tanksAi[tanksAi.length] = { x: Rx, y: 0, i: 0, v: tankAiModel[Rform].v, h: tankAiModel[Rform].h, inter: tankAiModel[Rform].inter, t: tankAiModel[Rform].t, d: Rd, l: tankAiModel[Rform].l, color: tankAiModel[Rform].color };
}

//驱动
tankGame.prototype.gameHandle = function () {
    var self = this;
    //清空画板上的数据
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.draw(this.context);
    //更新计分器
    this.updateMark();
    if (!this.gameover) {
        setTimeout(function () {
            self.gameHandle()
        }, 20);
    }
}

//AI坦克射击
tankGame.prototype.AIshot = function (i) {
    var tanksAi = this.tanksAi;
    var bullets = this.bullets;
    if (tanksAi[i].i == 0) {
        tanksAi[i].i = 1;
        this.shotInterval(i, tanksAi);
        bullets[bullets.length] = { x: tanksAi[i].x + tanksAi[i].l * 3 / 8, y: tanksAi[i].y + tanksAi[i].l * 3 / 8, l: tanksAi[i].l / 4, v: tanksAi[i].bv, color: 'rgb(255,0,0)', belong: 0, d: tanksAi[i].d };
        if (tanksAi[i].d == 1) {
            bullets[bullets.length - 1].y = tanksAi[i].y - tanksAi[i].l / 4;
            this.bulletsMove(i, bullets[bullets.length - 1], 0, -1, bullets.length - 1);
        } else if (tanksAi[i].d == 2) {
            bullets[bullets.length - 1].x = tanksAi[i].x + tanksAi[i].l;
            this.bulletsMove(i, bullets[bullets.length - 1], 1, 0, bullets.length - 1);
        } else if (tanksAi[i].d == 3) {
            bullets[bullets.length - 1].y = tanksAi[i].y + tanksAi[i].l;
            this.bulletsMove(i, bullets[bullets.length - 1], 0, 1, bullets.length - 1);
        } else if (tanksAi[i].d == 4) {
            bullets[bullets.length - 1].x = tanksAi[i].x - tanksAi[i].l / 4;
            this.bulletsMove(i, bullets[bullets.length - 1], -1, 0, bullets.length - 1);
        }
    }
}

//炮弹的移动 (炮弹所属，要移动的炮弹，炮弹在x,y正方向上的移动，（0:不移动,1:向正方向移动,-1:向反方向移动），炮弹的索引)
tankGame.prototype.bulletsMove = function (belong, bullet, dx, dy, index) {
    var self = this;
    var bullets = this.bullets;
    var tanks = this.tanks;
    var tanksAi = this.tanksAi;
    bullet.x += dx;
    bullet.y += dy;
    if (bullet.belong != 0) {
        for (var t = 0, tlen = tanksAi.length; t < tlen; t++) {
            if (this.judgeCrash(bullet.x, bullet.y, bullet.l, tanksAi[t].x, tanksAi[t].y, tanksAi[t].l)) {
                bullet.x = 0 - bullet.l;
                bullet.y = 0 - bullet.l;
                tanksAi[t].h--;
                switch (tanksAi[t].h) {
                    case 0: tanksAi[t].color = 'rgb(204,204,204)'; break;
                    case 1: tanksAi[t].color = 'rgb(153,51,51)'; break;
                    default: ;
                }
                if (tanksAi[t].h < 0) {
                    this.kill[bullet.belong - 1]++;
                    this.kills++;
                    tanksAi[t].l = 0;
                    tanksAi[t].x = this.canvas.width + t;
                }
                if (this.kills >= this.wins) {
                    alert('you win');
                    alert('player1:' + this.kill[0] + '\nplayer2:' + this.kill[1]);
                    this.gameover = true;
                    this.kills = 0;
                }
                break;
            }
        }
    } else if (bullet.belong == 0) {
        for (var p = 0, plen = tanks.length; p < plen; p++) {
            if (this.judgeCrash(bullet.x, bullet.y, bullet.l, tanks[p].x, tanks[p].y, tanks[p].l)) {
                bullet.x = -bullet.l;
                bullet.y = -bullet.l;
                tanks[p].h--;
                tanks[p].x = this.tanksPosition[p].x;
                tanks[p].y = this.tanksPosition[p].y;
                if (tanks[p].h < 0)
                    tanks.splice(p, 1);
                if (tanks.length == 0) {
                    alert('game over');
                    this.gameover = true;
                }
                break;
            }
        }
    }
    for (var b = 0, blen = bullets.length; b < blen; b++) {
        if (index != b) {
            if (this.judgeCrash(bullet.x, bullet.y, bullet.l, bullets[b].x, bullets[b].y, bullets[b].l)) {
                bullet.x = -bullet.l;
                bullets[b].x = canvas.width + bullets[b].l + b;
                bullet.y = -bullet.l;
                bullets[b].y = canvas.height + bullets[b].l + b;
                break;
            }
        }
    }
    //for (var w = 0, wlen = woods[pic].length; w < wlen; w++) {
    //    if (this.judgeCrash(bullet.x, bullet.y, bullet.l, woods[pic][w].x * woodLength, woods[pic][w].y * woodLength, woodLength)) {
    //        bullet.x = -bullet.l;
    //        bullet.y = -bullet.l;
    //        woods[pic].splice(w, 1);
    //        break;
    //    }
    //}
    //for (var f = 0, flen = Fes[pic].length; f < flen; f++) {
    //    if (this.judgeCrash(bullet.x, bullet.y, bullet.l, Fes[pic][f].x * Felength, Fes[pic][f].y * Felength, Felength)) {
    //        bullet.x = -bullet.l;
    //        bullet.y = -bullet.l;
    //        break;
    //    }
    //}
    //for (var pr = 0, prlen = protects.length; pr < prlen; pr++) {
    //    if (this.judgeCrash(bullet.x, bullet.y, bullet.l, protects[pr].x * woodLength, protects[pr].y * woodLength, woodLength)) {
    //        bullet.x = -bullet.l;
    //        bullet.y = -bullet.l;
    //        protects.splice(pr, 1);
    //        break;
    //    }
    //}
    //子弹打到旗子
    //if (this.judgeCrash(bullet.x, bullet.y, bullet.l, (canvas.width + flagLength) / 2, canvas.height - flagLength, flagLength)) {
    //    bullet.x = -bullet.l;
    //    bullet.y = -bullet.l;
    //    alert('game over');
    //    gameover = true;
    //}
    //更新数据
    this.updateData(tanks, bullets, tanksAi);
    if (!this.outOfcContainer(bullet.x, bullet.y, bullet.l, 0, 0, canvas.width, canvas.height))
        setTimeout(function () {
            self.bulletsMove(belong, bullet, dx, dy, index)
        }, bullets[0].t);
    else {
        bullet.x = -bullet.l;
        bullet.y = -bullet.l;
    }
}

//道具生效
tankGame.prototype.propsApply = function (index) {
    var props = this.props;
    var objTanks = this.tanks;
    if (props[0].t == 0) {
        if (objTanks[index].l <= 40) {
            objTanks[index].l += 5;
        }
    } else if (props[0].t == 1) {
        if (objTanks[index].l >= 15) {
            objTanks[index].l -= 5;
        }
    } else if (props[0].t == 2) {
        if (objTanks[index].h <= 4) {
            objTanks[index].h++;
            if (getElementsByClassName(document, 'h').length > 1)
                getElementsByClassName(document, 'h')[index].innerHTML = 'h:' + objTanks[index].h;
            else
                getElementsByClassName(document, 'h')[index].innerHTML = 'h:' + objTanks[0].h;
        }
    } else if (props[0].t == 3) {
        if (objTanks[index].v <= 8)
            objTanks[index].v++;
    } else if (props[0].t == 4) {
        if (objTanks[index].bv < 12)
            objTanks[index].bv++;
    } else if (props[0].t == 5) {
        if (objTanks[index].inter >= 300)
            objTanks[index].inter -= 50;
    }
}

//标记道具
tankGame.prototype.markProp = function () {
    var self = this;
    var propLength = this.propLength;
    var randomProp = this.randomProp;
    var props = this.props;
    setTimeout(function () {
        var randomx = Math.floor(Math.random() * (520 - propLength)),
            randomy = Math.floor(Math.random() * (520 - propLength)),
            randomt = Math.floor(Math.random() * randomProp.length);
        props[props.length] = { x: randomx, y: randomy, t: randomt };
        setTimeout(function () { props.length = 0 }, 8000);
        self.markProp();
    }, 30000 - $("#propsFreq_con").val() * 1000);
}

//主程序结构
tankGame.prototype.mainModel = function () {
    var self = this;
    var oMark = document.getElementById('mark_con');//分数显示框 击杀坦克的数量
    //初始化画地图
    this.context.cirRct(100, 100, 40, 40, 2, 'rgb(102,102,102)');
    oMark.innerHTML = 0;
    //驱动
    this.gameHandle(this.context, this.canvas);
    //道具系统
    this.markProp();
    document.onkeydown = function (event) {
        var event = event || window.event;
        self.ikeydown(event.keyCode);
        event.preventDefault();
        event.stopPropagation();
        return;
    }
}

//页面加载
window.onload = function () {
    tankgame.dynamicPage();
    tankgame.mainModel();
}
